package com.example.floristshop;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.floristshop.database.User;
import com.example.floristshop.utils.InitDatabaseUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private RadioGroup tabRadioGroup;
    private List<Fragment> fragmentList;
    private FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences init_preference = getSharedPreferences("init", MODE_PRIVATE);
        Boolean init_done = init_preference.getBoolean("init_done", false);
        Log.d("lyc", "onCreate: init_done = "+init_done);
        if(!init_done){
            InitDatabaseUtil.initDatabase();
            SharedPreferences.Editor editor = getSharedPreferences("init", MODE_PRIVATE).edit();
            editor.putBoolean("init_done", true);
            editor.apply();
        }

        SharedPreferences user_preferences = getSharedPreferences("user", MODE_PRIVATE);
        Boolean is_login = user_preferences.getBoolean("is_login", false);
        if(!is_login){
            Toast.makeText(MainActivity.this, "请登录", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
        initView();
    }


    //初始化三个Fragment
    private void initView(){
        //1. find view
        viewPager = (ViewPager)findViewById(R.id.fragment_vp);
        tabRadioGroup = (RadioGroup)findViewById(R.id.tabs_rg);

        //2. init viewPager
        fragmentList = new ArrayList<Fragment>(3);
        fragmentList.add(new RecordFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new UserInfoFragment());
        fragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton)tabRadioGroup.getChildAt(position);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //3. init radioGroup
        tabRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < group.getChildCount(); i++) {
                    if (group.getChildAt(i).getId() == checkedId) {
                        viewPager.setCurrentItem(i);
                    }
                }
            }
        });

        //4. set first fragment
        tabRadioGroup.check(R.id.search_tab);

    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter{

        private List<Fragment> mlist;

        public MyFragmentPagerAdapter(@NonNull FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.mlist = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return this.mlist==null?null:(this.mlist).get(position);
        }

        @Override
        public int getCount() {
            return this.mlist==null?0:(this.mlist).size();
        }
    }
}